package com.oo.app.ui.main.activity.girl;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.oo.app.R;
import com.oo.app.base.activities.BaseActivity;
import com.oo.app.ui.dialog.LoginSelectSexDialog;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @创建者 喻亚龙
 * @描述 自拍认证
 */

public class AuthenticationActivity extends BaseActivity implements View.OnClickListener{

    private static final int REQUEST_CAMERA_CODE = 1;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.iv_image1)
    ImageView ivImage1;

    @BindView(R.id.ll_image)
    LinearLayout llImage;

    @BindView(R.id.iv_image2)
    ImageView ivImage2;

    @BindView(R.id.iv_image3)
    ImageView ivImage3;

    @BindView(R.id.tv_content)
    TextView tvContent;

    @BindView(R.id.btn_authentication)
    Button btnAuthentication;

    @BindView(R.id.btn_repeat)
    Button btnRepeat;

    private boolean isauthentication = false;

    //调用照相机返回图片临时文件
    public File tempFile;

    public static void show(Context context) {
        Intent intent = new Intent(context, AuthenticationActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getContentView() {
        return R.layout.activity_authentication;
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        tvTitle.setText("自拍认证");

        //创建拍照存储的临时文件
        tempFile = createCameraTempFile(getSavedInstanceState());
    }

    @OnClick({R.id.iv_back,R.id.btn_authentication,R.id.btn_repeat})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                if (isauthentication){
                    isauthentication = false;
                    llImage.setVisibility(View.GONE);
                    btnRepeat.setVisibility(View.GONE);
                    ivImage1.setVisibility(View.VISIBLE);
                    tvContent.setText(R.string.authentication_text);
                    btnAuthentication.setText("立即认证");
                }else {
                    finish();
                }

                break;
            case R.id.btn_authentication:
                if (isauthentication){
                    authentication();
                }else {
                    Camera();
                }
                break;
            case R.id.btn_repeat:
                Camera();

                break;
        }
    }


    /**
     * 提交弹框
     */
    public void authentication(){
        final LoginSelectSexDialog loginSelectSexDialog = new LoginSelectSexDialog(AuthenticationActivity.this,"认证审核中。。。","OO官方会尽快告知你认证结果","完成",true);
        loginSelectSexDialog.show();
        loginSelectSexDialog.setClicklistener(new LoginSelectSexDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                finish();
                loginSelectSexDialog.dismiss();
            }

            @Override
            public void doCancel() {

            }
        });
    }

    public void Camera() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra("android.intent.extras.CAMERA_FACING", 1); // 调用前置摄像头
        intent.putExtra("autofocus", true); // 自动对焦
        intent.putExtra("fullScreen", false); // 全屏
        intent.putExtra("showActionIcons", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        startActivityForResult(intent, REQUEST_CAMERA_CODE);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("tempFile", tempFile);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CAMERA_CODE && resultCode == RESULT_OK) {
            String path = tempFile.getPath();
            Bitmap photo = BitmapFactory.decodeFile(path);

            ivImage3.setImageBitmap(photo);
            isauthentication = true;
            llImage.setVisibility(View.VISIBLE);
            btnRepeat.setVisibility(View.VISIBLE);
            ivImage1.setVisibility(View.GONE);
            tvContent.setText(R.string.authentication_text2);
            btnAuthentication.setText("提交");
            btnRepeat.setText("重拍");
        }
    }


    /**
     * 创建调用系统照相机待存储的临时文件
     *
     * @param savedInstanceState
     */
    public File createCameraTempFile(Bundle savedInstanceState) {
        if (savedInstanceState != null && savedInstanceState.containsKey("tempFile")) {
            tempFile = (File) savedInstanceState.getSerializable("tempFile");
        } else {
            tempFile = new File(checkDirPath(Environment.getExternalStorageDirectory().getPath() + "/image/"),
                    System.currentTimeMillis() + ".jpg");
        }
        return tempFile;
    }

    /**
     * 检查文件是否存在
     */
    private static String checkDirPath(String dirPath) {
        if (TextUtils.isEmpty(dirPath)) {
            return "";
        }
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dirPath;
    }
}
